import request from "@/utils/axios";
import constant from "@/utils/constant";
import appName from "@/api/appName";

// 获取水印内容
export function getWatermark(userId) {
  console.log("获取水印内容请求参数: " + userId);
  return request({
    url: appName.TASK + '/cdfs/auth/getWatermark',
    method: 'post',
    data: {
      "userId": userId
    }
  })
}

const sunCMImage = {
  /**
   * 显示CM影像（调用IE内核展示）
   * @param {} busParam 业务参数
   * @param {} imgServerUrl  影像地址
   */
  showImage: function(busParam,imgServerUrl){


    var BASE_URL = this.getBaseURL();// 获取上下文路径

    // var url= BASE_URL+'/imageDisplay.html?str='+busParam+'&imgServerUrl='+encodeURIComponent(imgServerUrl);
    //
    // url="SunPrint://type=ie&url="+encodeURIComponent(window.location.origin+url);
    //
    //
    // window.open(url,"_blank");

    let userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    let downloadPower = sessionStorage.getItem("downloadPower");
    var watermark = userInfo.id;
    getWatermark(userInfo.id).then(res => {
      if (res.resultCode == constant.SUCCESS) {
        watermark = watermark + res.data;
      }

      console.log("水印内容: "  + watermark);
      var url='sunlinealert://'+window.location.host +BASE_URL+'/imageDisplay.html?str='+busParam+'&imgServerUrl='+encodeURIComponent(imgServerUrl)+'&downloadPower='+downloadPower+'&watermark='+watermark;
      window.open(url,"_blank");
    }).catch(err => {
      console.log(err);
      console.log("水印内容: "  + watermark);
      var url='sunlinealert://'+window.location.host +BASE_URL+'/imageDisplay.html?str='+busParam+'&imgServerUrl='+encodeURIComponent(imgServerUrl)+'&downloadPower='+downloadPower+'&watermark='+watermark;
      window.open(url,"_blank");
    });
  } ,
  /**
   * 获取上下文路径
   */
  getBaseURL:function(){
    var BASE_URL=process.env.BASE_URL;
    if(BASE_URL.substring(BASE_URL.length-1)=='/'){
      BASE_URL=BASE_URL.substring(0,BASE_URL.length-1)
    }
    return BASE_URL;
  }
}

export default sunCMImage;
